﻿using RBTools.CodeGenerator.CodeDomGenerator.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml;
using System.CodeDom;
using System.IO;
using System.CodeDom.Compiler;
using System.Diagnostics;

namespace RBTools.CodeGenerator.CodeDomGenerator.Test
{
    
    
    /// <summary>
    ///This is a test class for MessageCodeGeneratorTest and is intended
    ///to contain all MessageCodeGeneratorTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MessageCodeGeneratorTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for BuildCodeCompileUnit
        ///</summary>
        [TestMethod()] 
        public void BuildCodeCompileUnitTest()
        { 
            MessageCodeGenerator target = new MessageCodeGenerator();  
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("E:\\Messages.xml");
            target.DefaultNamespace = "RBTools.CodeGenerator.CodeDomGenerator";
            CodeCompileUnit codeCompileUnit = target.BuildCodeCompileUnit(xmlDoc);

            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
            CodeGeneratorOptions options =new CodeGeneratorOptions ();
            string generateCSFileName = ("E:\\Messages.cs"); 
            if (File.Exists(generateCSFileName))
            {
                File.Delete(generateCSFileName);
            }
            using (StreamWriter sWriter = new StreamWriter(generateCSFileName))
            {
                provider.GenerateCodeFromCompileUnit(codeCompileUnit, sWriter, options);
            }
            Process.Start(generateCSFileName);
        }

        /// <summary>
        ///A test for GenerateCode
        ///</summary>
        [TestMethod()]
        [DeploymentItem("RBTools.CodeGenerator.CodeDomGenerator.dll")]
        public void GenerateCodeTest()
        {
            MessageCodeGenerator_Accessor target = new MessageCodeGenerator_Accessor();
            target.DefaultNamespace = "RBTools.CodeGenerator.CodeDomGenerator";
            string inputFileName = string.Empty;
            string inputFileContent = string.Empty;

            using (StreamReader sReader = new StreamReader("E:\\Messages.xml"))
            {
                inputFileContent = sReader.ReadToEnd();
                sReader.Close();
            }            
            byte[] actual = target.GenerateCode(inputFileName, inputFileContent);
        }
    }
}
